package com.cdpm.web.controller.common;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.cdpm.common.core.controller.BaseController;
import com.cdpm.common.core.page.TableDataInfo;
import com.cdpm.common.utils.CollectionUtil;
import com.cdpm.common.utils.DateUtils;
import com.cdpm.common.utils.StringUtils;
import com.cdpm.common.utils.bean.BeanUtils;
import com.cdpm.system.domain.ActivityConfig;
import com.cdpm.system.service.IActivityService;
import com.cdpm.system.service.IStrategyService;
import com.cdpm.web.dto.*;
import com.cdpm.web.dto.req.*;
import com.cdpm.web.dto.res.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description:活动配置请求类
 * @author: longjy
 * @create: 2024-08-20 10:27
 **/
@RestController
@RequestMapping("/activity")
public class ActivityController extends BaseController {
    @Resource
    IStrategyService iStrategyService;

    @Resource
    IActivityService iActivityService;
    /**
     * 活动列表查询
     */
    @PostMapping("/queryActivityList")
    public QueryActivityListResDTO queryActivityList(@RequestBody QueryActivityListReqDTO queryActivityListReqDTO){
        QueryActivityListResDTO queryActivityListResDTO = new QueryActivityListResDTO();
        startPage();
        List<ActivityConfigDTO> list = new ArrayList<>();
        ActivityConfig activityConfig = new ActivityConfig();
        BeanUtils.copyBeanProp(activityConfig,queryActivityListReqDTO);
        activityConfig.setIsDelete("0");
        if (queryActivityListReqDTO.getActivityId()!=null) {
            activityConfig.setActivityId(Long.parseLong(queryActivityListReqDTO.getActivityId()));
        }
        List<ActivityConfig> activityConfigs = iActivityService.findList(activityConfig);
//        List<ActivityConfig> activityConfigs = iActivityService.list(new QueryWrapper<>(activityConfig) );
        TableDataInfo dataTable = getDataTable(activityConfigs);
        queryActivityListResDTO.setRows(dataTable.getRows());
        queryActivityListResDTO.setTotal(dataTable.getTotal());
        queryActivityListResDTO.setMsg(dataTable.getMsg());
        queryActivityListResDTO.setCode(dataTable.getCode());
        if(CollectionUtil.isNotEmpty(queryActivityListResDTO.getRows())){
            List<ActivityConfig> activityConfigs1 = (List<ActivityConfig>)queryActivityListResDTO.getRows();
            for (ActivityConfig activityConfig1 : activityConfigs1) {
                ActivityConfigDTO activityConfigDTO = new ActivityConfigDTO();
                BeanUtils.copyBeanProp(activityConfigDTO,activityConfig1);
                activityConfigDTO.setActivityStartDateStr(DateUtils.parseDateToStr(activityConfigDTO.getActivityStartDate(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setActivityEndDateStr(DateUtils.parseDateToStr(activityConfigDTO.getActivityEndDate(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setInsertTimeStr(DateUtils.parseDateToStr(activityConfigDTO.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setUpdateTimeStr(DateUtils.parseDateToStr(activityConfigDTO.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));

                activityConfigDTO.setValidTimeStartStr(DateUtils.parseDateToStr(activityConfigDTO.getValidTimeStart(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setValidTimeEndStr(DateUtils.parseDateToStr(activityConfigDTO.getValidTimeEnd(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setActiveTimeStartStr(DateUtils.parseDateToStr(activityConfigDTO.getActiveTimeStart(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setActiveTimeEndStr(DateUtils.parseDateToStr(activityConfigDTO.getActiveTimeEnd(),"yyyy-MM-dd HH:mm:ss"));
                activityConfigDTO.setPrizeTimeStartStr(DateUtils.parseDateToStr(activityConfigDTO.getPrizeTimeStart(),"HH:mm:ss"));
                activityConfigDTO.setPrizeTimeEndStr(DateUtils.parseDateToStr(activityConfigDTO.getPrizeTimeEnd(),"HH:mm:ss"));
                //渠道名称
                activityConfigDTO.setChannelName(ENUM_CHANNEL_TYPE.getLabelByValue(activityConfig1.getChannelCode()));
                //活动类型名称
                activityConfigDTO.setActivityTypeName(ENUM_ACTIVITY_TYPE.getLabelByValue(activityConfig1.getActivityType()));
                activityConfigDTO.setPrizeMethodName(ENUM_PRIZE_METHOD.getLabelByValue(activityConfig1.getPrizeMethod()));
                activityConfigDTO.setUserInfoScanName(ENUM_USER_INFO_SCAN.getLabelByValue(activityConfig1.getUserInfoScan()));

                if(StringUtils.isNotEmpty(activityConfig1.getUserInfoMsg())){
                    List msgStr = Arrays.asList(activityConfig1.getUserInfoMsg().split(","));
                    activityConfigDTO.setUserInfoMsgStr(msgStr);
                }

                activityConfigDTO.setPrizeLimitName(ENUM_PRIZE_LIMIT.getLabelByValue(activityConfig1.getPrizeLimit()));
                activityConfigDTO.setUserRightName(ENUM_USER_RIGHT.getLabelByValue(activityConfig1.getUserRight()));
                activityConfigDTO.setTemplateName(ENUM_TEMPLATE.getLabelByValue(activityConfig1.getTemplate()));
                activityConfigDTO.setSkinName(ENUM_SKIN.getLabelByValue(activityConfig1.getSkin()));

                if(activityConfig1.getActivityId()!=null){
                    activityConfigDTO.setActivityId(activityConfig1.getActivityId().toString());
                }
                list.add(activityConfigDTO);
            }
        }
        queryActivityListResDTO.setRows(list);
        return queryActivityListResDTO;
    }

    /**
     * 活动配置详情查询
     */
    @PostMapping("/queryActivityById")
    public QueryActivityByIdResDTO queryActivityById(@RequestBody QueryActivityByIdReqDTO queryActivityByIdReqDTO){
        QueryActivityByIdResDTO queryActivityByIdResDTO = new QueryActivityByIdResDTO();
        long activityId = Long.parseLong(queryActivityByIdReqDTO.getActivityId());
//        ActivityConfig activityConfig = iActivityService.findById(activityId);
        ActivityConfig activityConfig = iActivityService.getById(activityId);
        if(activityConfig!=null){
            BeanUtils.copyBeanProp(queryActivityByIdResDTO,activityConfig);
            queryActivityByIdResDTO.setActivityId(activityConfig.getActivityId()+"");
            queryActivityByIdResDTO.setActivityStartDateStr(DateUtils.parseDateToStr(activityConfig.getActivityStartDate(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setActivityEndDateStr(DateUtils.parseDateToStr(activityConfig.getActivityEndDate(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setInsertTimeStr(DateUtils.parseDateToStr(activityConfig.getInsertTime(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setUpdateTimeStr(DateUtils.parseDateToStr(activityConfig.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));

            queryActivityByIdResDTO.setValidTimeStartStr(DateUtils.parseDateToStr(activityConfig.getValidTimeStart(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setValidTimeEndStr(DateUtils.parseDateToStr(activityConfig.getValidTimeEnd(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setActiveTimeStartStr(DateUtils.parseDateToStr(activityConfig.getActiveTimeStart(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setActiveTimeEndStr(DateUtils.parseDateToStr(activityConfig.getActiveTimeEnd(),"yyyy-MM-dd HH:mm:ss"));
            queryActivityByIdResDTO.setPrizeTimeStartStr(DateUtils.parseDateToStr(activityConfig.getPrizeTimeStart(),"HH:mm:ss"));
            queryActivityByIdResDTO.setPrizeTimeEndStr(DateUtils.parseDateToStr(activityConfig.getPrizeTimeEnd(),"HH:mm:ss"));

            if(StringUtils.isNotEmpty(activityConfig.getUserInfoMsg())){
                List msgStr = Arrays.asList(activityConfig.getUserInfoMsg().split(","));
                queryActivityByIdResDTO.setUserInfoMsgStr(msgStr);
            }
            //活动类型名称
            queryActivityByIdResDTO.setActivityTypeName(ENUM_ACTIVITY_TYPE.getLabelByValue(activityConfig.getActivityType()));
            if(activityConfig.getActivityId()!=null){
                queryActivityByIdResDTO.setActivityId(activityConfig.getActivityId().toString());
            }
        }
        return queryActivityByIdResDTO;
    }

    /**
     * 新增或更新活动配置
     */
    @PostMapping("/insertOrUpdateActivity")
    public InsertOrUpdateActivityResDTO insertOrUpdateActivity(@RequestBody InsertOrUpdateActivityReqDTO insertOrUpdateActivityReqDTO){
        InsertOrUpdateActivityResDTO insertOrUpdateActivityResDTO = new InsertOrUpdateActivityResDTO();
        ActivityConfig activityConfig = new ActivityConfig();
        BeanUtils.copyBeanProp(activityConfig,insertOrUpdateActivityReqDTO);
        String activityStartDateStr = insertOrUpdateActivityReqDTO.getActivityStartDateStr();
        String activityEndDateStr = insertOrUpdateActivityReqDTO.getActivityEndDateStr();
        if(StringUtils.isNotEmpty(activityStartDateStr)){
            Date date = DateUtils.parseStrDate(activityStartDateStr, "yyyy-MM-dd HH:mm:ss");
            activityConfig.setActivityStartDate(date);
        }
        if(StringUtils.isNotEmpty(activityEndDateStr)){
            Date date = DateUtils.parseStrDate(activityEndDateStr, "yyyy-MM-dd HH:mm:ss");
            activityConfig.setActivityEndDate(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getActivityId())){
            activityConfig.setActivityId(Long.parseLong(insertOrUpdateActivityReqDTO.getActivityId()));
        }

        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getValidTimeStartStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getValidTimeStartStr(), "yyyy-MM-dd HH:mm:ss");
            activityConfig.setValidTimeStart(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getValidTimeEndStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getValidTimeEndStr(), "yyyy-MM-dd HH:mm:ss");
            activityConfig.setValidTimeEnd(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getActiveTimeStartStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getActiveTimeStartStr(), "yyyy-MM-dd HH:mm:ss");
            activityConfig.setActiveTimeStart(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getActiveTimeEndStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getActiveTimeEndStr(), "yyyy-MM-dd HH:mm:ss");
            activityConfig.setActiveTimeEnd(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getPrizeTimeStartStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getPrizeTimeStartStr(), "HH:mm:ss");
            activityConfig.setPrizeTimeStart(date);
        }
        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getPrizeTimeEndStr())){
            Date date = DateUtils.parseStrDate(insertOrUpdateActivityReqDTO.getPrizeTimeEndStr(), "HH:mm:ss");
            activityConfig.setPrizeTimeEnd(date);
        }
        if(insertOrUpdateActivityReqDTO.getUserInfoMsgStr() != null && insertOrUpdateActivityReqDTO.getUserInfoMsgStr().size()>0){
            List<String> userInfoMsgStr = insertOrUpdateActivityReqDTO.getUserInfoMsgStr();
            String ss = "";
            for(String str:userInfoMsgStr){
                ss += str + ",";
            }
            String substring = ss.substring(0, ss.length() - 1);
            activityConfig.setUserInfoMsg(substring);
        }


        if(StringUtils.isNotEmpty(insertOrUpdateActivityReqDTO.getActivityId())){
            //更新
//            int i = iActivityService.update(activityConfig);
            iActivityService.saveOrUpdate(activityConfig);
            insertOrUpdateActivityResDTO.setCode(200);
            HashMap<String, String> map = new HashMap<>();
            map.put("activityId",activityConfig.getActivityId()+"");
            insertOrUpdateActivityResDTO.setMsg(JSON.toJSONString(map));
        }else{
            //新增
            activityConfig.setIsDelete("0");
//            int i = iActivityService.insert(activityConfig);
            iActivityService.save(activityConfig);
            Long activityId = activityConfig.getActivityId();
            HashMap<String, String> map = new HashMap<>();
            map.put("activityId",activityId.toString());
            insertOrUpdateActivityResDTO.setCode(200);
            insertOrUpdateActivityResDTO.setMsg(JSON.toJSONString(map));
        }
        return insertOrUpdateActivityResDTO;
    }

    @PostMapping("/queryEnumList")
    public QueryEnumListResDTO queryEnumList(@RequestBody QueryEnumListReqDTO queryEnumListReqDTO){
        QueryEnumListResDTO queryStrategyListResDTO = new QueryEnumListResDTO();
        Map<String,Object> map = new HashMap<>();
        map.put("channelType",ENUM_CHANNEL_TYPE.getEnumList());
        map.put("customerGroup", ENUM_CUSTOMER_GROUP_TYPE.getEnumList());
        map.put("activityType", ENUM_ACTIVITY_TYPE.getEnumList());
        map.put("strategyActionType", ENUM_STRATEGY_ACTION_TYPE.getEnumList());
        map.put("strategyDoType", ENUM_STRATEGY_DO_TYPE.getEnumList());
        map.put("strategyExecuteType", ENUM_STRATEGY_EXECUTE_TYPE.getEnumList());
        map.put("strategyNodeType", ENUM_STRATEGY_NODE_TYPE.getEnumList());
        map.put("strategyStatus", ENUM_STRATEGY_STATUS_TYPE.getEnumList());
        map.put("prizeLevel", ENUM_PRIZE_LEVEL.getEnumList());

        map.put("enumPrizeMethod", ENUM_PRIZE_METHOD.getEnumList());//奖品兑换方式
        map.put("enumUserInfoScan", ENUM_USER_INFO_SCAN.getEnumList());//用户信息填写
        map.put("enumUserInfoMsg", ENUM_USER_INFO_MSG.getEnumList());//用户信息
        map.put("enumPrizeLimit", ENUM_PRIZE_LIMIT.getEnumList());//抽奖次数限制
        map.put("enumUserRight", ENUM_USER_RIGHT.getEnumList());//用户权限
        map.put("enumTemplate", ENUM_TEMPLATE.getEnumList());//模板
        map.put("enumSkin", ENUM_SKIN.getEnumList());//皮肤


        List<OrganInfoDTO> organInfoDTOS = JSONArray.parseArray(EnumOverallInfo.organList, OrganInfoDTO.class);
        map.put("organList", JSONArray.toJSONString(organInfoDTOS));
        queryStrategyListResDTO.setEnumList(map);
        return queryStrategyListResDTO;
    }

}
